package com.example.azheng.rxjavamvpdemo.ui.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.base.BaseMvpFragment;
import com.example.azheng.rxjavamvpdemo.bean.BaseArrayBean;
import com.example.azheng.rxjavamvpdemo.bean.rechargelist.RechargeResponseData;
import com.example.azheng.rxjavamvpdemo.mvp.contract.RechargeRecordContract;
import com.example.azheng.rxjavamvpdemo.mvp.presenter.RechargeRecordPresenter;
import com.example.azheng.rxjavamvpdemo.ui.adapter.RechargeListAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Time:2020/5/19
 * Author:ypf
 * Description:
 */
public class RechargeRecordFragment extends BaseMvpFragment<RechargeRecordPresenter> implements RechargeRecordContract.View, RechargeListAdapter.OnItemClickListener {


    @BindView(R.id.rvRechargerRecord)
    RecyclerView rvRechargerRecord;
    @BindView(R.id.srlRechargerRecord)
    SmartRefreshLayout srlRechargerRecord;

    private int currentPage = 1;

    private List<RechargeResponseData> mData;

    private RechargeListAdapter mRechargeListAdapter;

    @Override
    protected void initView(View view) {
        mPresenter = new RechargeRecordPresenter();
        mPresenter.attachView(this);
        mData = new ArrayList<>();
        mRechargeListAdapter = new RechargeListAdapter(getContext(), mData);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        rvRechargerRecord.setLayoutManager(linearLayoutManager);
        rvRechargerRecord.setAdapter(mRechargeListAdapter);
        mRechargeListAdapter.setmOnItemClickListener(this);


        srlRechargerRecord.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mData.clear();
                mPresenter.rechargelist(currentPage);
            }
        });
        srlRechargerRecord.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ++currentPage;
                mPresenter.rechargelist(currentPage);
            }
        });
        srlRechargerRecord.autoRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recharge_record;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    /**
     * 充值列表请求成功
     *
     * @param bean 数据
     */
    @Override
    public void rechargelistSuccess(BaseArrayBean<RechargeResponseData> bean) {
        finishRefresh();

        if (null == bean) {
            return;
        }

        if (null == bean || 0 == bean.getData().size()) {
            return;
        }

        mData.addAll(bean.getData());
        mRechargeListAdapter.notifyDataSetChanged();
    }

    /**
     * 完成上下来刷新
     */
    private void finishRefresh() {

        if (1 == currentPage) {
            srlRechargerRecord.finishRefresh();
        } else {
            srlRechargerRecord.finishLoadMore();
        }
    }


    /**
     * 列表点击事件
     *
     * @param position 位置
     */
    @Override
    public void click(int position) {

    }

}
